package com.tool.main;

import com.tool.bean.TableGenInfo;
import com.tool.util.DbUtil;
import com.tool.util.StringUtil;
import java.io.PrintStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DbReader
{
	
	public static Map getParams(TableGenInfo info)
		throws Exception
	{
		Map result = new HashMap();
		String tableName = info.getTableName();
		result.put("Bean", StringUtil.getBeanName(tableName));
		result.put("entityTable", tableName) ;
		result.put("bean", StringUtil.getCamel(tableName));
		result.put("package", info.getPackageName());
		result.put("TABLE", "`" + tableName.toUpperCase() + "`");
		result.put("auto_increment", info.getAutoIncrement());
		
		List fields = new ArrayList();
		StringBuffer cols = new StringBuffer();
		StringBuffer _cols = new StringBuffer();
		StringBuffer item_cols = new StringBuffer();
		StringBuffer update_sql = new StringBuffer();
		List columns = new ArrayList();
		Map<String, String> columnCommons = new HashMap<String, String>();
		Connection conn = null;
		List list = null;
		try
		{
			conn = DbUtil.getConnection(info.getDsName());
			list = DbUtil.getColumns(conn, tableName);
			columnCommons = DbUtil.getColumnsCommon(conn, tableName);
			
			conn.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		for (int i = 0; i < list.size(); i++)
		{
			String[] tmps = ((String) list.get(i)).split(",");
			String col = tmps[0];
			if (!"ID".equals(col.toUpperCase()))
			{
				String javaType = tmps[1];
				Map field = new HashMap();
				String name = StringUtil.getCamel(col);
				
				field.put("name", name);
				field.put("Name", StringUtil.getBeanName(col));
				field.put("col", col);
				field.put("javaType", javaType);
				
				String columnComment = name;
				if (columnCommons.containsKey(name))
					columnComment = columnCommons.get(name);
				field.put("columnComment", columnComment);
				
				fields.add(field);
				
				if (i < list.size() - 1)
				{
					cols.append("`").append(col).append("`,");
					_cols.append("#{").append(name).append("},");
					item_cols.append("#{item.").append(name).append("},");
					update_sql.append("`").append(col).append("`=")
							.append("#{").append(name).append("},");
				}
				else
				{
					cols.append("`").append(col).append("`");
					_cols.append("#{").append(name).append("}");
					item_cols.append("#{item.").append(name).append("}");
					update_sql.append("`").append(col).append("`=")
							.append("#{").append(name).append("}");
				}
			}
			
			String colName = StringUtil.getCamel(col);
			Map column = new HashMap();
			column.put("columnName", col);
			column.put("typeName", colName);
			column.put("propertyName", colName);
			
			String columnComment = colName;
			if (columnCommons.containsKey(colName))
				columnComment = columnCommons.get(colName);
			column.put("columnComment", columnComment);
			columns.add(column);
		}
		result.put("fields", fields);
		result.put("COLS", cols.toString());
		result.put("_COLS", _cols.toString());
		result.put("ITEM_COLS", item_cols.toString());
		result.put("UPDATE_SQL", update_sql.toString());
		
		result.put("columns", columns);
		System.out.println("Load Config Success");
		return result;
	}
}
